#!/usr/bin/env bash

set -e

# Extracts version numbers from a passed in git tag.
# Handles both chainlink and explorer style git tags.
# gittag2dockertag <gittag>
# i.e:
# gittag2dockertag v0.7.1 -> 0.7.1
# gittag2dockertag explorer-v0.7.1 -> 0.7.1

if [ -z "$1" ]
then
  exit 0 # No tag, skip.
fi

gittag=`echo $1 | awk '{print tolower($0)}'`

# Extract version from git tags for chainlink releases
# e.g v0.7.1 -> 0.7.1
if [[ "${gittag}" =~ ^v([a-zA-Z0-9.]+) ]]; then
  echo ${BASH_REMATCH[1]}
# Extract version from git tags for explorer releases
# e.g. explorer-v0.7.1 -> 0.7.1
elif [[ "${gittag}" =~ ^explorer-v([a-zA-Z0-9.]+) ]]; then
  echo "${BASH_REMATCH[1]}"
else
  echo "unable to deduce docker tag from git tag ${gittag}"
  exit 1
fi
